package org.wangzs.blog.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Component;
import org.wangzs.blog.dao.BaseDAO;
import org.wangzs.blog.dao.inner.IPostDAO;
import org.wangzs.blog.entity.Page;
import org.wangzs.blog.entity.TPost;
import org.wangzs.blog.util.Const;


@Component("PostDAOImpl")
public class PostDAOImpl extends BaseDAO implements IPostDAO {
	
	private static final Logger log = LoggerFactory.getLogger(PostDAOImpl.class);
	
	@Override
	@Cacheable("default")
	public TPost get(String postId) {
		String sql = "select * from post where deletedFlag=:deletedFlag and id=:id order by createdTime desc";
		System.out.println(sql);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("deletedFlag", Const.FLAG_NO);
		paramMap.put("id", postId);
		List<TPost> list = this.getNamed().query(sql, paramMap, new BeanPropertyRowMapper<TPost>(TPost.class));
		return list.size()>0?list.get(0):null;
	}
	
	@Override
	public Page<TPost> get(int pageSize, int pageNum) {
		int start = pageSize*(pageNum-1);
		String sql = "select * from post where deletedFlag=:deletedFlag and publishedFLag=:publishedFLag order by createdTime desc limit :start, :pageSize";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("deletedFlag", Const.FLAG_NO);
		paramMap.put("publishedFLag", Const.FLAG_YES);
		paramMap.put("pageSize", pageSize);
		paramMap.put("start", start);
		List<TPost> list = this.getNamed().query(sql, paramMap, new BeanPropertyRowMapper<TPost>(TPost.class));
		
		Page<TPost> page = new Page<TPost>();
		page.setPageSize(pageSize);
		page.setPageNum(pageNum);
		page.setTotalNum(this.getTotalNum());
		page.setResult(list);
		
		return page;
	}
	
	@Override
	public int save(TPost post) throws DataAccessException {
		String sql = "insert into post values(:id, :title, :content, :createdTime, :modifiedTime, :deletedTime, :deletedFlag)";
		post.setId(null);
		SqlParameterSource ps = new BeanPropertySqlParameterSource(post);
		return this.getNamed().update(sql, ps);
	}

	@Override
	@CacheEvict(value="default", key="#post.getId()")
	public int update(TPost post) {
		String sql = "update post set title=:title, content=:content, createdTime=:createdTime, modifiedTime=:modifiedTime, deletedFlag=:deletedFlag, deletedTime=:deletedTime where id=:id";
		SqlParameterSource ps = new BeanPropertySqlParameterSource(post);
		return this.getNamed().update(sql, ps);
	}

	@Override
	public int delete(String postId) {
		String sql = "update post set deletedFlag=:deletedFlag1, deletedTime=:deletedTime where id=:id and  deletedFlag=:deletedFlag2";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("deletedFlag1", Const.FLAG_YES);
		paramMap.put("deletedTime", new Date());
		paramMap.put("id", postId);
		paramMap.put("deletedFlag2", Const.FLAG_NO);
		log.debug(sql);
		return this.getNamed().update(sql, paramMap);
	}

	@Override
	public int getTotalNum() {
		String sql = "select count(id) from post where deletedFlag=:deletedFlag";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("deletedFlag", Const.FLAG_NO);
		return this.getNamed().queryForObject(sql, paramMap, Integer.class);
	}

}
